package com.boot.frame.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.boot.constant.Constants;
import com.boot.exception.BusinessException;
import com.boot.utils.SerializableUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Jedis是Redis的Java连接开发工具
 *
 * @author 【zhouJie】
 */
@Component
public class JedisOperate {

    private static final Logger logger = LoggerFactory.getLogger(JedisOperate.class);

    private static JedisPool jedisPool;

    //TODO 静态注入jedisPool
    @Autowired
    public void setJedisPool(JedisPool jedisPool) {
        JedisOperate.jedisPool = jedisPool;
    }

    /**
     * 获取Jedis实例
     *
     * @return
     */
    public static synchronized Jedis getJedis() {
        try {
            if (jedisPool != null) {
                return jedisPool.getResource();
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("获取Jedis实例失败");
            throw new BusinessException("Redis实例失败");
        }
    }

    /**
     * 释放Jedis资源
     */
    public static void closePool() {
        try {
            jedisPool.close();
        } catch (Exception e) {
            logger.error("释放Jedis资源失败");
            throw new BusinessException("Redis释放失败");
        }
    }

    /**
     * 通过redis-key获取object
     *
     * @param key
     * @return
     */
    public static Object getObject(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            byte[] bytes = jedis.get(key.getBytes());
            if (bytes.length > 0) {
                return SerializableUtil.unserializable(bytes);
            }
        } catch (Exception e) {
            logger.error("通过redis-key获取object失败, 验证码失效了!");
            throw new BusinessException("验证码失效了!");
        }
        return null;
    }

    /**
     * 向redis存入[key-object]
     *
     * @param key
     * @param value
     * @return
     */
    public static String setObject(String key, Object value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.set(key.getBytes(), SerializableUtil.serializable(value));
        } catch (Exception e) {
            logger.error("向redis存入[key-object]失败");
            throw new BusinessException("Redis保存失败");
        }
    }

    /**
     * 向redis存入[key-object]+过期时间
     *
     * @param key
     * @param value
     * @param expiresTime
     * @return
     */
    public static String setObject(String key, Object value, int expiresTime) {
        String result;
        try (Jedis jedis = jedisPool.getResource()) {
            result = jedis.set(key.getBytes(), SerializableUtil.serializable(value));
            if (Constants.OK.equals(result)) {
                jedis.expire(key.getBytes(), expiresTime);
            }
            return result;
        } catch (Exception e) {
            logger.error("向redis存入[key-object]+过期时间失败");
            throw new BusinessException("Redis保存失败");
        }
    }

    /**
     * 通过redis-key获取json
     *
     * @param key
     * @return
     */
    public static String getJson(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            logger.error("通过redis-key获取json失败");
            throw new BusinessException("验证码失效了!");
        }
    }

    /**
     * 向reids存入[key-json]
     *
     * @param key
     * @param value
     * @return
     */
    public static String setJson(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.set(key, value);
        } catch (Exception e) {
            logger.error("向reids存入[key-json]失败");
            throw new BusinessException("Redis保存失败");
        }
    }

    /**
     * 向reids存入[key-json]+过期时间
     *
     * @param key
     * @param value
     * @param expiresTime
     * @return
     */
    public static String setJson(String key, String value, int expiresTime) {
        String result;
        try (Jedis jedis = jedisPool.getResource()) {
            result = jedis.set(key, value);
            if (Constants.OK.equals(result)) {
                jedis.expire(key, expiresTime);
            }
            return result;
        } catch (Exception e) {
            logger.error("向reids存入[key-json]+过期时间失败");
            throw new BusinessException("Redis保存失败");
        }
    }

    /**
     * 删除redis-key
     *
     * @param key
     * @return
     */
    public static Long delKey(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.del(key.getBytes());
        } catch (Exception e) {
            logger.error("删除redis-key失败");
            throw new BusinessException("Redis删除失败");
        }
    }

    /**
     * key是否存在
     *
     * @param key
     * @return
     */
    public static Boolean exists(String key) {
        Jedis jedis = jedisPool.getResource();
        return jedis.exists(key.getBytes());
    }

}
